package cn.it.ygh.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import cn.it.ygh.annotation.SystemLog;
import cn.it.ygh.pojo.Agent;
import cn.it.ygh.pojo.House;
import cn.it.ygh.pojo.User;
import cn.it.ygh.service.AgentManageService;
import cn.it.ygh.service.HouseManageService;

@Controller
@RequestMapping("/agentManage")
public class AgentManageController {
	@Resource
	private AgentManageService agentManageService;
	
	@RequestMapping("/getAllAgent")
	public ModelAndView getAllAgent()
	{
		ModelAndView modelAndView = new ModelAndView();
		List<Agent> agentList= agentManageService.getAllAgent();
		modelAndView.addObject("agentList",agentList);
		modelAndView.setViewName("../admin/jsp/agents-list");
		return modelAndView;
	}
	
	@RequestMapping("/getAgentById.do")
	@SystemLog(description="查看中介的详细信息")
	public ModelAndView getAgentById(@RequestParam(value="agentId") int agentId,HttpSession session)
	{
		ModelAndView modelAndView = new ModelAndView();
		Agent agent = agentManageService.getAgentById(agentId);
		System.out.println(agent.getAgentAccount());
		session.setAttribute("temp", agent);
		modelAndView.setViewName("../admin/jsp/agents-list");
		return modelAndView;
	}
	@RequestMapping("/deleteAgent.do")
	@SystemLog(description="删除中介")
	public ModelAndView deleteAgent(@RequestParam(value="agentId") int agentId)
	{
		ModelAndView modelAndView = new ModelAndView();
		System.out.println(agentId);
		agentManageService.deleteAgent(agentId);
		modelAndView.setViewName("../admin/jsp/agents-list");
		return modelAndView;
	}
	@RequestMapping("/batchdeleteAgent.do")
	@SystemLog(description="批量删除中介")
	public ModelAndView batchdeleteAgent(@RequestParam(value="agentIds") String agentIds)
	{
		ModelAndView modelAndView = new ModelAndView();
		String str[] = agentIds.split("#");
		int[] ids = new int[str.length];
		for(int i = 0;i < str.length;i++)
			ids[i] = Integer.parseInt(str[i]);
		agentManageService.batchdeleteAgent(ids);
		modelAndView.setViewName("../admin/jsp/agents-list");
		return modelAndView;
	}
	@SystemLog(description="添加中介")
	@RequestMapping(value="/addAgent.do",method=RequestMethod.POST)
	public ModelAndView addAgent(Agent agent){
		System.out.println("----");
		ModelAndView modelAndView = new ModelAndView();
		//user.setUserTime(new Date());
		agentManageService.addAgent(agent);
		modelAndView.setViewName("../admin/jsp/agents-list");
		return modelAndView;
	}
	@SystemLog(description="查找中介")
	@RequestMapping(value="/findAgent.do",method=RequestMethod.POST)
	public ModelAndView findAgent(@RequestParam(value = "key") String key)
	{
		ModelAndView modelAndView = new ModelAndView();
		List<Agent> agentList = agentManageService.findAgent(key);
		modelAndView.addObject("agentList", agentList);
		modelAndView.setViewName("../admin/jsp/agents-list");
		return modelAndView;
	}
	
	@RequestMapping(value="/modifyAgent.do",method=RequestMethod.POST)
	@SystemLog(description="修改中介信息")
	public ModelAndView modifyUser(Agent agent){
		System.out.println("---");
		ModelAndView modelAndView = new ModelAndView();
		System.out.println(agent.getAgentAccount());
		agentManageService.modifyAgent(agent);
		modelAndView.setViewName("../admin/jsp/agents-list");
		return modelAndView;
	}
	@RequestMapping(value="/getPageData.do",method=RequestMethod.GET)
	@SystemLog(description="查看所有中介信息")
	@ResponseBody
	public Map<Object, Object> getPageData(@RequestParam(value = "curr") int curr,@RequestParam(value = "pageSize") int pageSize,@RequestParam(value = "key") String key)
	{
		List<Agent> agentList = agentManageService.getPageData((curr-1)*pageSize, pageSize,key);
		System.out.println(curr);
		System.out.println(pageSize);
		System.out.println(key);
		int totalRecord = agentManageService.getTotalRecord(key);
		Map<Object, Object> info = new HashMap<Object, Object>();
		info.put("data", agentList);
		info.put("total", totalRecord);
		JSONArray array = new JSONArray();
		array.addAll(agentList);
		System.out.println(array.toString());
		return info;
	}
}
